package ru.smc.hogwarts.servlets.professors;

import java.io.IOException;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Query;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import ru.smc.hogwarts.classes.EMF;
import ru.smc.hogwarts.model.Person;

public class ProfessorsList extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private HttpSession session;
       
    public ProfessorsList() {
        super();
    }
    
	@SuppressWarnings("unchecked")
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session = request.getSession(true);
    	
    	String action = request.getParameter("search");
    					
		if (action != null && action.equals("clear") || (session.getAttribute("servletName") != null && !session.getAttribute("servletName").equals(getServletName()))) {
			session.setAttribute("searchFN", "");
			session.setAttribute("searchLN", "");
	    	session.setAttribute("searchH", "");
		}
		
		session.setAttribute("servletName", getServletName());
    			
    	String select = "select p " +
    			"from Person p " +
    			"where (p.role.name = 'professor' " +
    			"or p.role.name = 'head' " +
    			"or p.role.name = 'head_house')";
    	
    	if ((session.getAttribute("searchFN") != null) && (session.getAttribute("searchFN") != "")) 
    		select += " and p.firstName like '%" + session.getAttribute("searchFN") + "%'";
    	if ((session.getAttribute("searchLN") != null) && (session.getAttribute("searchLN") != "")) 
    		select += "and p.lastName like '%" + session.getAttribute("searchLN") + "%'";
    	if ((session.getAttribute("searchH") != null) && (session.getAttribute("searchH") != "")) 
    		select += "and p.house.name = '" + session.getAttribute("searchH") + "'";    	
    	
    	EntityManagerFactory emf = EMF.getInstance().getEMF();
    	EntityManager em = emf.createEntityManager();
    	
    	Query query = em.createQuery(select);
    	int size = query.getResultList().size();
    	
    	request.setAttribute("size", size);
    	
    	if (size != 0) {    	
	    	action = request.getParameter("page");
	    	
	    	if (action == null) {
	    		session.setAttribute("pageBegin", 0);
	    		session.setAttribute("pageEnd", 19);
	    	} else {
	    		int begin = 0;
	    		int end = 19;
	    		
	    		try {
	    			begin = (Integer)session.getAttribute("pageBegin");
	    			end = (Integer)session.getAttribute("pageEnd");
	    		} catch (NullPointerException e) {}
	    		
	    		if (action.equals("next") && end+1 < size) {
	    			begin += 20;
	    			end += 20;
	    		} else if (action.equals("previous") && (begin > 0)) {
	    			begin -= 20;
	    			end -= 20;
	    		}
	    		
	    		session.setAttribute("pageBegin", begin);
	    		session.setAttribute("pageEnd", end);
	    	}
	    	
	    	query.setFirstResult((Integer)session.getAttribute("pageBegin"));
	    	query.setMaxResults(20);
	    	List<Person> professors = em.createQuery(select).getResultList();
	    	request.setAttribute("professors", professors);
    	}  	
    	
    	request.getRequestDispatcher("/pages/professors/professorsList.jsp").forward(request, response);
    	
    	em.close();    	
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		session.setAttribute("searchFN", request.getParameter("searchFN"));
		session.setAttribute("searchLN", request.getParameter("searchLN"));
		session.setAttribute("searchH", request.getParameter("searchH"));
		
		this.doGet(request, response);
	}

}
